home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / oop.swg / 0042_String List Object.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-08-25  |  5.1 KB  |  232 lines

  1. UNIT filelist;
  2. {
  3.   Contains Object List for keeping a list of files.
  4. }
  5. INTERFACE
  6. USES DOS, OPString;
  7.  
  8. TYPE  CmdPtr = ^CmdRec;
  9.       CmdRec = RECORD
  10.           CmdStr : PathStr;  {79 char to allow for maximum path length}
  11.           Next   : CmdPtr;
  12.       end;
  13.  
  14.       List   = OBJECT
  15.           First, Last, Current : CmdPtr;
  16.           ListCount : Word;
  17.  
  18.           CONSTRUCTOR Init;
  19.           Procedure AddName( Name : String );
  20.           Procedure SortList;
  21.           Procedure SortListReverse;
  22.           Function Compare( A, B : String ) : Boolean;
  23.           Function FirstName : String;
  24.           Function LastName : String;
  25.           Function CurrentName : String;
  26.           Function NextName : String;
  27.           Function TotalCount : Word;
  28.           Procedure ClearList;
  29.           Function InList( Name : String; CheckCase : Boolean ) : Boolean;
  30.           DESTRUCTOR Done;
  31.       END;
  32.  
  33. IMPLEMENTATION
  34.  
  35. CONSTRUCTOR LIST.INIT;
  36. BEGIN
  37.   FIRST := NIL;
  38.   LAST := NIL;
  39.   CURRENT := NIL;
  40.   LISTCOUNT := 0;
  41. END;
  42.  
  43. PROCEDURE LIST.ADDNAME( NAME : STRING );
  44.   { Add a new CmdRec to the list }
  45. VAR
  46.   TempCmdPtr : CmdPtr;
  47. BEGIN
  48.   NEW(TempCmdPtr);
  49.   If First = NIL then begin
  50.     First := TempCmdPtr;
  51.     Current := TempCmdPtr;
  52.   end else
  53.     Last^.Next := TempCmdPtr;
  54.   TempCmdPtr^.Next := NIL;
  55.   TempCmdPtr^.CmdStr := Name;
  56.   Last := TempCmdPtr;
  57.   INC(ListCount);
  58. END;
  59.  
  60. PROCEDURE LIST.SORTLIST;
  61. VAR
  62.   TempCmdPtr : CmdPtr;
  63.   P, Q : CmdPtr;
  64. BEGIN
  65.   if (First = NIL) or (First^.Next = NIL) then EXIT;
  66.   TempCmdPtr := First;
  67.   First := First^.Next;
  68.   TempCmdPtr^.Next := Nil;
  69.  
  70.   repeat
  71.      p := TempCmdPtr;
  72.  
  73.      if not Compare( p^.CmdStr, First^.CmdStr ) then
  74.         begin
  75.           TempCmdPtr := First;
  76.           First := First^.Next;
  77.           TempCmdPtr^.Next := p;
  78.         end
  79.      else
  80.      begin
  81.        while (compare( p^.CmdStr, First^.CmdStr ) AND
  82.              (p <> NIL)) do
  83.        begin
  84.          q := p;
  85.          p := p^.Next;
  86.        end;
  87.  
  88.        if p = NIL then
  89.        begin
  90.          p := First;
  91.          First := First^.Next;
  92.          q^.Next := p;
  93.          p^.Next := NIL;
  94.        end
  95.          else
  96.        begin
  97.          q^.next := First;
  98.          First := First^.next;
  99.          q^.next^.next := p;
  100.        end;
  101.      end;
  102.   until First = NIL;
  103.  
  104.   First := TempCmdPtr;
  105.   Current := First;
  106.   Last := First;
  107.  
  108.   repeat
  109.   Last := Last^.Next;
  110.   until Last^.Next = NIL;
  111.  
  112. END;
  113.  
  114. PROCEDURE LIST.SORTLISTREVERSE;
  115. VAR
  116.   TempCmdPtr : CmdPtr;
  117.   CheckPtr   : CmdPtr;
  118.   tempstr    : string;
  119. BEGIN
  120.   if (First = NIL) or (First^.Next = NIL) then EXIT;
  121.   TempCmdPtr := First;
  122.   CheckPtr := First^.Next;
  123.  
  124.   While (TempCmdPtr <> NIL) DO
  125.   BEGIN
  126.     While (CheckPtr <> NIL) DO
  127.     BEGIN
  128.       { if the tempcmdptr string is less then the checkptr string }
  129.       If compare(TempCmdPtr^.CmdStr, CheckPtr^.CmdStr) then
  130.       BEGIN
  131.         { then swap the strings }
  132.         tempstr := tempCmdPtr^.cmdstr;           { save temp's string }
  133.         TempCmdPtr^.cmdStr := CheckPtr^.Cmdstr; { assign check's string to temp
  134.         CheckPtr^.Cmdstr := tempstr;            { assign tempptr's string to ch
  135.       end;
  136.       CheckPtr := Checkptr^.next;               { get a pointer to next node }
  137.     end; { while checkptr }
  138.     TempCmdPtr := TempCmdPtr^.Next;             { get the next compairson base 
  139.   end; { while tempcmdptr }
  140. end; { SortListReverse }
  141.  
  142. FUNCTION LIST.COMPARE( A, B : String ) : BOOLEAN;
  143. begin
  144.   Compare := (CompUCString( A,B ) = Less);
  145. end;
  146.  
  147.  
  148. FUNCTION LIST.FIRSTNAME : String;
  149. BEGIN
  150.   if First <> NIL then begin
  151.     FirstName := First^.CmdStr;
  152.     Current := First;
  153.   end else
  154.     FirstName := '';
  155. END;
  156.  
  157. FUNCTION LIST.LASTNAME : String;
  158. BEGIN
  159.   if Last <> NIL then begin
  160.     LastName := Last^.CmdStr;
  161.     Current := Last;
  162.   end else
  163.     LastName := '';
  164. END;
  165.  
  166. FUNCTION LIST.CURRENTNAME : String;
  167. BEGIN
  168.   if Current <> NIL then
  169.     CurrentName := Current^.CmdStr
  170.   else
  171.     CurrentName := '';
  172. END;
  173.  
  174. FUNCTION LIST.NEXTNAME : String;
  175. BEGIN
  176.   if (Current <> NIL) Then begin
  177.     Current := Current^.Next;
  178.     if (Current <> NIL) then
  179.       NextName := Current^.CmdStr
  180.     else
  181.       NextName := '';
  182.   end else
  183.     NextName := '';
  184. END;
  185.  
  186. FUNCTION LIST.TOTALCOUNT : Word;
  187. BEGIN
  188.   TotalCount := ListCount;
  189. END;
  190.  
  191. PROCEDURE LIST.CLEARLIST;
  192. BEGIN
  193.   if First <> NIL then
  194.     repeat
  195.       Current := First^.Next;
  196.       Dispose(First);
  197.       First := Current;
  198.     until First = nil;
  199.   Last := First;
  200.   ListCount := 0;
  201. END;
  202.  
  203. Function List.InList(Name:String; CheckCase : Boolean) : Boolean;
  204. { returns true if string was in list }
  205. VAR
  206.   TempPtr : CmdPtr;
  207.   OK      : Boolean;
  208. BEGIN
  209.   Ok := false;
  210.   TempPtr := Current;
  211.   Current := First;
  212.   If checkCase then OK := (CompString(FirstName,Name) = Equal)
  213.   Else Ok := (CompUCString(FirstName,Name) = Equal);
  214.   If Not OK then
  215.   BEGIN
  216.     While (Current <> Nil) AND Not OK DO
  217.     If CheckCase then OK := (CompString(NextName,Name) = Equal)
  218.     Else OK := (CompUCString(NextName,Name) = Equal);
  219.   end;
  220.   InList := OK;
  221.   Current := TempPtr;
  222. end;
  223.  
  224. DESTRUCTOR LIST.DONE;
  225. BEGIN
  226.   ClearList;
  227. END;
  228.  
  229. BEGIN
  230. END.
  231.  
  232.